# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=graphite2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3.14
pkgrel=2
pkgdesc="Font rendering capabilities for complex non-Roman writing systems (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://github.com/silnrsi/graphite"
license=('LGPL2.1+')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python")
options=('strip' 'staticlibs')
source=("https://github.com/silnrsi/graphite/releases/download/${pkgver}/${_realname}-${pkgver}.tgz"
        "001-graphite2-1.3.12-win64.patch"
        "002-graphite2-1.2.1-pkgconfig.patch"
        "003-graphite2-1.3.12-staticbuild.patch"
        "004-graphite2-1.3.12-dllimport-fix.patch"
)
sha256sums=('f99d1c13aa5fa296898a181dff9b82fb25f6cc0933dbaa7a475d8109bd54209d'
            'a77a3e21099454b0732da2ec37067c871c267e9443a9c6c56ca44245e6a64c4f'
            '1792cf52964090ecd144c8cfd94de0e5e2438b0bac1e66fd81a872626d4aff70'
            '7cfc9f8bcb786b89713cfd043bc471ef61441360fa6b45b5fc2e93cd69e79e93'
            'cc33eab2ac0e335963d678ce7af15ccfef4cbc94e6722ffeb99b200f14a5003d')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  apply_patch_with_msg 001-graphite2-1.3.12-win64.patch \
    002-graphite2-1.2.1-pkgconfig.patch \
    003-graphite2-1.3.12-staticbuild.patch \
    004-graphite2-1.3.12-dllimport-fix.patch
}

check() {
  cd "${srcdir}"/build-${CARCH}
  # we also want to use mingw-w64-x86_64 and mingw-w64-i686 versions of python
  mkdir -p "${srcdir}"/${_realname}-${pkgver}/tests/backup
  cp "${srcdir}"/${_realname}-${pkgver}/tests/{corrupt.py,defuzz,fnttxtrender,hbspeeds,jsoncmp,trace2svg} \
   ${srcdir}"/${_realname}-${pkgver}/tests/backup"
  sed -i "s:\/usr\/bin\/python:${MINGW_PREFIX}\/bin\/python:" \
   "${srcdir}"/${_realname}-${pkgver}/tests/{corrupt.py,defuzz,fnttxtrender,hbspeeds,jsoncmp,trace2svg}
  ctest || true
  rm "${srcdir}"/${_realname}-${pkgver}/tests/{corrupt.py,defuzz,fnttxtrender,hbspeeds,jsoncmp,trace2svg}
  cp "${srcdir}"/${_realname}-${pkgver}/tests/backup/* "${srcdir}"/${_realname}-${pkgver}/tests
  rm -rf "${srcdir}"/${_realname}-${pkgver}/tests/backup
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  # C++17 is needed
  CXXFLAGS="${CXXFLAGS} -std=gnu++17" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DGRAPHITE2_COMPARE_RENDERER=ON \
      ../${_realname}-${pkgver}

  make
}


package() {
  cd "${srcdir}"/build-${CARCH}
  make DESTDIR=${pkgdir} install

  # License
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
